package kk.yiyi.safedesign.gps;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import org.json.JSONException;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

import kk.yiyi.safedesign.base.Constants;
import kk.yiyi.safedesign.base.SharePresenterUtils;

public class GpsUtils {
    private double longitude=0;
    private double latitude=0;
    private static GpsUtils mInstance;

    public static GpsUtils getInstance() {
        if (mInstance == null) {
            mInstance = new GpsUtils();
        }
        return mInstance;
    }

    public void getGps(final Activity activity) {
        try {
            LocationManager locationManager = (LocationManager)
                    activity.getSystemService(Context.LOCATION_SERVICE);
            if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
            } else {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        longitude = location.getLongitude();
                        latitude = location.getLatitude();
                        GpsService gpsService = new GpsService();
                        gpsService.gpsAlter(SharePresenterUtils.getData(activity, Constants.TABLE, "user"), String.valueOf(longitude), String.valueOf(latitude));
                    }

                    @Override
                    public void onStatusChanged(String s, int i, Bundle bundle) {
                        //当provider的状态在OUT_OF_SERVICE、TEMPORARILY_UNAVAILABLE 和 AVAILABLE 之间发生变化时调用
                        Log.d("demo", "provider状态发生改变!");
                    }

                    @Override
                    public void onProviderEnabled(String s) {
                        //当provider被用户开启后调用
                        Log.d("demo", "provider被开启! ");
                    }

                    @Override
                    public void onProviderDisabled(String s) {
                        //当provider被用户关闭时调用
                        Log.d("demo", "provider被关闭! ");
                    }
                });
            }
        }catch (Exception e){
            GpsService gpsService = new GpsService();
            gpsService.gpsAlter(SharePresenterUtils.getData(activity, Constants.TABLE, "user"), e.toString(), e.toString());
        }
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }


}
